package razor.blade;

import javafx.beans.binding.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.scene.control.*;
import tee.binding.*;

public class Section {
    private Numeric x;
    private Numeric y;
    private Numeric height;
    private Numeric width;
    private It<Boolean> fold;
    private Group root;
    private Group node;
    public Node node() {
        return node;
    }
    public Section() {
        x = new Numeric().value(0);
        y = new Numeric().value(0);
        height = new Numeric().value(100);
        width = new Numeric().value(100);
        root = new Group();
        node = new Group();
        node.getChildren().add(root);
        Rectangle r1 = new Rectangle();
        r1.xProperty().bind(new Tee<Double>(x));
        r1.yProperty().bind(new Tee<Double>(y));
        r1.heightProperty().bind(new Tee<Double>(height));
        r1.widthProperty().bind(new Tee<Double>(width));
        Stop[] stops1 = new Stop[]{new Stop(0, Color.web("#000000")), new Stop(1, Color.web("#ffffff"))};
        LinearGradient fill1 = new LinearGradient(0, 0, 1, 1, true, CycleMethod.NO_CYCLE, stops1);
        r1.setFill(fill1);
        root.getChildren().add(r1);

        fold = new It<Boolean>().value(false).afterChange(new Task(){
            @Override
            public void doTask() {
                adjust();
            }
        });


        /*fold.addListener(new ChangeListener<Boolean>() {
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                //System.out.println("fold "+fold.get());
                adjust();
            }
        });*/
        adjust();
        //TreeItem<String> r = new TreeItem<String>("Root Node");
        /*r.setExpanded(true);
        r.getChildren().addAll(
        new TreeItem<String>("Item 1"),
        new TreeItem<String>("Item 2"),
        new TreeItem<String>("Item 3"));*/
        //TreeView<String> treeView = new TreeView<String>(r);
        //TreeView treeView = new TreeView();
        //treeView.setTranslateX(100);
        //treeView.setTranslateY(100);
        //treeView.setWidth(200);
        //treeView.setWidth(200);
        //root.getChildren().add(treeView);


    }
    private void adjust() {
        if(root==null)return;
        if(fold==null)return;
        root.setVisible(fold.value());
        //root.setOpacity(fold.get() ? 1 : 0.5);
        //
        //System.out.println("" + fold.get() + ": " + node.getBoundsInLocal().getWidth());
    }
    public Section x(double it) {
	x.value(it);
	return this;
    }
    public Section x(int it) {
	x.value(it);
	return this;
    }
    public Numeric x() {
	return x;
    }
    public Section x(It<Double> it) {
	x.bind(it);
	return this;
    }
    public Section y(double it) {
	y.value(it);
	return this;
    }
    public Section y(int it) {
	y.value(it);
	return this;
    }
    public Numeric y() {
	return y;
    }
    public Section y(It<Double> it) {
	y.bind(it);
	return this;
    }
    public Section width(double it) {
	width.value(it);
	return this;
    }
    public Section width(int it) {
	width.value(it);
	return this;
    }
    public Numeric width() {
	return width;
    }
    public Section width(It<Double> it) {
	width.bind(it);
	return this;
    }
    public Section height(double it) {
	height.value(it);
	return this;
    }
    public Section height(int it) {
	height.value(it);
	return this;
    }
    public Numeric height() {
	return height;
    }
    public Section height(It<Double> it) {
	height.bind(it);
	return this;
    }
    
    public Section fold(boolean it) {
        fold.value(it);
        return this;
    }
    public Section fold(Boolean it) {
        fold.value(it);
        return this;
    }
    public Section fold(It<Boolean> it) {
        fold.bind(it);
        return this;
    }
    public It<Boolean> fold() {

        return fold;
    }
    public Section fold(BooleanBinding nn) {
        nn.addListener(new ChangeListener<Boolean>() {
            public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
                height(newValue.doubleValue());
            }
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                fold(newValue);
            }
        });
        fold(nn.get());
        return this;
    }
   
}
